package sg.simple.snmp.provider.impl;

import isnmp.ui.dto.ProgramInfo;
import isnmp.ui.dto.ProgramInfo.PROGRAM_PROPERTY;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.TableEvent;

public class ProgramInfoProvider extends SNMPTableReader {

	/** Conceptual entry to mark the index in table */
	private String programTableIndexOid = "1.3.6.1.2.1.25.4.2.1.1";

	private String programNameOid = "1.3.6.1.2.1.25.4.2.1.2";

	private String programPathOid = "1.3.6.1.2.1.25.4.2.1.4";

	private String programRunParametersOid = "1.3.6.1.2.1.25.4.2.1.5";

	private String programTypeOid = "1.3.6.1.2.1.25.4.2.1.6";

	public List<ProgramInfo> retrieve(Snmp session, Target target) {

		OID programTableIndex = new OID(programTableIndexOid);
		OID programName = new OID(programNameOid);
		OID programPath = new OID(programPathOid);
		OID programRunParameters = new OID(programRunParametersOid);
		OID programType = new OID(programTypeOid);

		List<ProgramInfo> programInformationList = new ArrayList<ProgramInfo>();

		List<TableEvent> programInfoTable = this.readTable(session, target,
				new OID[] { programTableIndex, programName, programPath,
						programRunParameters, programType });

		for (TableEvent aRow : programInfoTable) {

			if (aRow.isError()) {
				continue;
			}

			String index = "";
			String name = "";
			String path = "";
			String params = "";
			String type = "";

			for (VariableBinding v : aRow.getColumns()) {

				if (matches(programTableIndex, v)) {
					index = v.getVariable().toString();
					continue;
				}

				if (matches(programName, v)) {
					name = v.getVariable().toString();
					continue;
				}

				if (matches(programPath, v)) {
					path = v.getVariable().toString();
					continue;
				}

				if (matches(programRunParameters, v)) {
					params = v.getVariable().toString();
					continue;
				}

				if (matches(programType, v)) {
					type = getProgramType(v.getVariable().toString());
					continue;
				}

			}

			ProgramInfo info = new ProgramInfo();
			info.putValue(PROGRAM_PROPERTY.INDEX, index);
			info.putValue(PROGRAM_PROPERTY.NAME, name);
			info.putValue(PROGRAM_PROPERTY.PATH, path);
			info.putValue(PROGRAM_PROPERTY.PARAMETERS, params);
			info.putValue(PROGRAM_PROPERTY.TYPE, type);
			programInformationList.add(info);
		}

		return programInformationList;

	}

	private String getProgramType(String code) {
		String ret = programType.get(code);
		return ret == null ? code : ret;

	}

	static Map<String, String> programType = new HashMap<String, String>();

	static {
		programType.put("1", "unknown");

		programType.put("2", "operatingSystem");
		programType.put("3", "deviceDriver");
		programType.put("4", "application");
	}

}
